import 'package:ezviz_flutter_plugin/constants.dart';
import 'package:ezviz_flutter_plugin/ezviz_camera_view.dart';
import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:ezviz_flutter_plugin/ezviz_flutter_plugin.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  var testFlutterDemo;

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      platformVersion = await EzvizFlutterPlugin.platformVersion;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    String platformVersion;

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: ListView(
          children: [
            // Container(height: 200, child: testView),
            EzvizCameraView(
              height: 200,
              accessToken: null,
              deviceSerial: null,
            ),
            FlatButton(
              onPressed: () async {
                // EzvizPlugin.init().then((value) => print('原生返回的数据是：$value ---- ${value is bool}'));
               EzvizFlutterPlugin.platformVersion;
              },
              child: Text('测试'),
            ),
            FlatButton(
              onPressed: () async {
                // EzvizPlugin.init().then((value) => print('原生返回的数据是：$value ---- ${value is bool}'));
                EzvizFlutterPlugin.startToPlay(
                  accessToken:
                  'at.du1mmksdbac93lmlc7bdrj08d0j8k6ho-1ou0fgevkf-1kpvzlk-xhie1bdci',
                  deviceSerial: 'F03174481',
                );
              },
              child: Text('初始化'),
            ),
            GestureDetector(
              onTapDown: (_) {
                EzvizFlutterPlugin.setDirection(EzvizPluginConstants.DIRECTION_UP);
              },
              onTapUp: (_) {
                EzvizFlutterPlugin.setDirection(
                  EzvizPluginConstants.DIRECTION_UP,
                  true,
                );
              },
              child: Container(
                color: Colors.red,
                padding: EdgeInsets.symmetric(vertical: 10),
                child: Text('按住上移'),
              ),
            ),
            GestureDetector(
              onTapDown: (_) {
                EzvizFlutterPlugin.setDirection(EzvizPluginConstants.DIRECTION_DOWN);
              },
              onTapUp: (_) {
                EzvizFlutterPlugin.setDirection(
                  EzvizPluginConstants.DIRECTION_DOWN,
                  true,
                );
              },
              child: Container(
                color: Colors.blue,
                padding: EdgeInsets.symmetric(vertical: 10),
                child: Text('按住下移'),
              ),
            ),
            GestureDetector(
              onTapDown: (_) {
                EzvizFlutterPlugin.setDirection(EzvizPluginConstants.DIRECTION_LEFT);
              },
              onTapUp: (_) {
                EzvizFlutterPlugin.setDirection(
                  EzvizPluginConstants.DIRECTION_LEFT,
                  true,
                );
              },
              child: Container(
                color: Colors.red,
                padding: EdgeInsets.symmetric(vertical: 10),
                child: Text('按住左移'),
              ),
            ),
            GestureDetector(
              onTapDown: (_) {
                EzvizFlutterPlugin.setDirection(EzvizPluginConstants.DIRECTION_RIGHT);
              },
              onTapUp: (_) {
                EzvizFlutterPlugin.setDirection(
                  EzvizPluginConstants.DIRECTION_RIGHT,
                  true,
                );
              },
              child: Container(
                color: Colors.blue,
                padding: EdgeInsets.symmetric(vertical: 10),
                child: Text('按住右移'),
              ),
            ),
            buildFlatButton(() => EzvizFlutterPlugin.closeSound(), "关闭声音"),
            buildFlatButton(() => EzvizFlutterPlugin.openSound(), "开启声音"),
          ],
        ),
      ),
    );
  }

  Widget buildFlatButton(Function onPressed, String text) {
    return FlatButton(
      onPressed: onPressed,
      child: Text(text),
    );
  }

  void onTestViewCreated(testFlutterPluginDemo) {
    this.testFlutterDemo = testFlutterPluginDemo;
  }
}
